﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.UserTypes;

namespace WebCommit.Common.Models
{
	/// <summary>
	/// 地理位置
	/// </summary>
	public class GeoPosition : IUserType
	{
		/// <summary>
		/// 维度
		/// </summary>
		public string Latitude
		{
			get;
			set;
		}
		/// <summary>
		/// 精度
		/// </summary>
		public string Longitude
		{
			get;
			set;
		}
		/// <summary>
		/// 国家
		/// </summary>
		public string Country
		{
			get;
			set;
		}
		/// <summary>
		/// 省
		/// </summary>
		public string Region
		{
			get;
			set;
		}
		/// <summary>
		/// 市
		/// </summary>
		public string City
		{
			get;
			set;
		}
		/// <summary>
		/// 区县
		/// </summary>
		public string Local
		{
			get;
			set;
		}

		public static GeoPosition Parse(string str)
		{
			string[] s = str.Split('|');
			GeoPosition g = new GeoPosition();
			g.Latitude = s.Length > 0 ? s[0] : "";
			g.Longitude = s.Length > 1 ? s[1] : "";
			g.Country = s.Length > 2 ? s[2] : "";
			g.Region = s.Length > 3 ? s[3] : "";
			g.City = s.Length > 4 ? s[4] : "";
			g.Local = s.Length > 5 ? s[5] : "";
			return g;
		}

		public override string ToString()
		{
			return Latitude + "|" + Longitude + "|" + Country + "|" + Region + "|" + City + "|" + Local;
		}
		/// <summary>
		/// 格式 {lat}{lng}{cy}{reg}{ct}{loc}
		/// </summary>
		/// <param name="format"></param>
		/// <returns></returns>
		public string ToString(string format)
		{
			return format.Replace("{lat}", Latitude).Replace("{lng}", Longitude).Replace("{cy}", Country).Replace("{reg}", Region).Replace("{ct}", City).Replace("{loc}", Local);
		}


		public NHibernate.SqlTypes.SqlType[] SqlTypes
		{
			get
			{
				return new NHibernate.SqlTypes.SqlType[] { new NHibernate.SqlTypes.SqlType(System.Data.DbType.String) };
			}
		}

		public Type ReturnedType
		{
			get
			{
				return typeof(GeoPosition);
			}
		}

		public bool Equals(object x, object y)
		{
			return x.GetHashCode() == y.GetHashCode();
		}

		public int GetHashCode(object x)
		{
			return x.ToString().GetHashCode();
		}

		public object NullSafeGet(System.Data.IDataReader rs, string[] names, object owner)
		{
			string value = (string)NHibernate.NHibernateUtil.String.NullSafeGet(rs, names[0]); ;
			if (value == null)
				return null;
			else
				return Parse(value);
		}

		public void NullSafeSet(System.Data.IDbCommand cmd, object value, int index)
		{
			if (value != null)
			{
				string str = value.ToString();
				NHibernate.NHibernateUtil.String.NullSafeSet(cmd, str, index);
			}
			else
				NHibernate.NHibernateUtil.String.NullSafeSet(cmd, value, index);
		}

		public object DeepCopy(object value)
		{
			GeoPosition rtn = new GeoPosition();
			GeoPosition ipt = value as GeoPosition;
			rtn.Latitude = ipt.Latitude;
			rtn.Longitude = ipt.Longitude;
			rtn.Country = ipt.Country;
			rtn.Region = ipt.Region;
			rtn.City = ipt.City;
			rtn.Local = ipt.Local;
			return rtn;
		}

		public bool IsMutable
		{
			get { return false; }
		}

		public object Replace(object original, object target, object owner)
		{
			return original;
		}

		public object Assemble(object cached, object owner)
		{
			return DeepCopy(cached);
		}

		public object Disassemble(object value)
		{
			return DeepCopy(value);
		}
	}
}
